﻿using B.S.Data.MES.API.Applications.CangKu.Command;
using B.S.Data.MES.API.Dto.CangKuDTO;
using B.S.Domain.CangKu;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.CangKu.CommandHandlers
{
    public class GetFenPeiCommandHandlers : IRequestHandler<GetFenpeiCommand, APIRequst<List<FenPeiDto>>>
    {
        private readonly IBaseRepository<CangKuinfo> cangkuRepository;
        private readonly IBaseRepository<CangKuQU> kuQuRepository;
        private readonly IBaseRepository<ChuWei> chuWeiRepository;
        private readonly IBaseRepository<RuKuinfo> rukuRepository;

        public GetFenPeiCommandHandlers(
            IBaseRepository<CangKuinfo> CangkuRepository,
            IBaseRepository<CangKuQU> KuQuRepository,
            IBaseRepository<ChuWei> ChuWeiRepository,
            IBaseRepository<RuKuinfo> rukuRepository)
        {
            cangkuRepository = CangkuRepository;
            kuQuRepository = KuQuRepository;
            chuWeiRepository = ChuWeiRepository;
            this.rukuRepository = rukuRepository;
        }

        public Task<APIRequst<List<FenPeiDto>>> Handle(GetFenpeiCommand request, CancellationToken cancellationToken)
        {
            var su = from d in rukuRepository.GetAll()
                     join a in cangkuRepository.GetAll() on d.CangId equals a.CangId into ca
                     from a in ca.DefaultIfEmpty()
                     join b in kuQuRepository.GetAll() on d.CangKuId equals b.CangKuId into kb
                     from b in kb.DefaultIfEmpty()
                     join c in chuWeiRepository.GetAll() on d.ChuId equals c.ChuId into cc
                     from c in cc.DefaultIfEmpty()
                     select new FenPeiDto
                     {
                         CangId = d.CangId,
                         CangkuNo = a.CangkuNo,
                         KuWeiId = c.KuWeiId,
                         CangKuId = d.CangKuId,
                         CangKuName = b.CangKuName,
                         BM = b.BM,
                         ChuId = d.ChuId,
                         ChuName = c.ChuName,
                         SID = d.SID,
                         ProductName = d.ProductName,
                         ProductStandard = d.ProductStandard,
                         ProductCount = d.ProductCount,
                         ProductColor = d.ProductColor,
                         RKState = d.RKState,
                         CangName = a.CangName,
                     };

            return Task.FromResult(new APIRequst<List<FenPeiDto>>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString(),
                Data = su.ToList()
            });
        }
    }
}
